package com.cykj.v_m_sprintboot.controller;

import com.alibaba.fastjson.JSON;
import com.cykj.v_m_sprintboot.bean.GoodsInfo;
import com.cykj.v_m_sprintboot.bean.TbOut;
import com.cykj.v_m_sprintboot.bean.TbReturn;
import com.cykj.v_m_sprintboot.bean.TbWarehouseListInf;
import com.cykj.v_m_sprintboot.service.TbWarehouseListInfService;
import com.cykj.v_m_sprintboot.tool.GetTime;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsInfController {
    @Autowired
    private TbWarehouseListInfService tbWarehouseListInfService;

    //根据条件查询仓库的信息
    @RequestMapping("/GoodsInf")
    @ResponseBody
    public Map<String, Object> GoodsInf(@RequestBody TbWarehouseListInf goodsInfo) {
        Map<String, Object> map = new HashMap<>();
        List<GoodsInfo> goodsInf = tbWarehouseListInfService.goodsInfo(goodsInfo);
        map.put("goodsInf", goodsInf);
        return map;
    }

    //查询所有仓库的信息
    @RequestMapping("/getAllGoods")
    @ResponseBody
    public Map<String, Object> getAllGoods(@RequestBody TbWarehouseListInf goodsInfo) {
        Map<String, Object> map = new HashMap<>();
        List<GoodsInfo> goodsInf = tbWarehouseListInfService.getAllGoods(goodsInfo);
        map.put("goodsInf", goodsInf);
        return map;
    }

    //退货信息的提交
    @RequestMapping("/addReturn")
    @ResponseBody
    public String addReturn(@RequestBody TbReturn tbReturn) {

        double totalNum = tbWarehouseListInfService.countTbWarehouseListInf(tbReturn);
        //单位的换算
        double num=0;
        if (tbReturn.getGoodsUnit().equals("瓶") ||tbReturn.getGoodsUnit().equals("包")){
            num=tbReturn.getReturnNum()/10;
        }else{
            num=tbReturn.getReturnNum();
        }
        double sum = (totalNum - num);
        System.out.println("仓库的数量为：" + totalNum + "," + "退货的数量为：" + num + "*******************************8");
        if (sum >= 0) {
            tbReturn.setGoodsNum(sum);
            tbReturn.setModifyTime(GetTime.getTime());
            tbWarehouseListInfService.updateTbWarehouseListInfNum(tbReturn);
            boolean flag = tbWarehouseListInfService.addReturn(tbReturn);
            if (flag == true) {
                return "提交成功";
            }
        }
        return "退货数量大于仓库数量";
    }

    //获取退货清单
    @RequestMapping("/getReturnList")
    @ResponseBody
    public Map<String, Object> getReturnList(@RequestBody TbReturn tbReturn) {
        tbReturn.setStateListId(3);
        RowBounds row = new RowBounds((tbReturn.getPage() - 1) * tbReturn.getSize(), tbReturn.getSize());
        Map<String, Object> map = new HashMap<>();
        List<TbReturn> returnList = tbWarehouseListInfService.getReturnList(tbReturn, row);
        map.put("count", tbWarehouseListInfService.countReturnList(tbReturn));
        map.put("returnList", returnList);
        return map;
    }

    //删除退货清单
    @RequestMapping("/delReturnList")
    @ResponseBody
    public String delReturnList(@RequestBody TbReturn tbReturn) {
        tbWarehouseListInfService.delReturnList(tbReturn);
        return "移除成功";
    }

    //确认退货，商品数据发生改变
    @RequestMapping("/sureReturnGoods")
    @ResponseBody
    public String sureReturnGoods(@RequestBody TbReturn tbReturn) {
        tbWarehouseListInfService.delReturnList(tbReturn);
        return "退货成功";
    }

}
